﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XeBus.BusinessLogic;
using XeBus.Controller;
//using XeBus.BusinessObject;

namespace XeBus.Controller
{
    public partial class frmTramDung : Form
    {
        private TramDungBUS tdBUS = new TramDungBUS();
        private DataTable dt = new DataTable();
        public string ma_con_duong { get; set; }
        private int y;

        public frmTramDung()
        {
            InitializeComponent();
            Load_Tram_Dung();
        }

        private void Load_Tram_Dung()
        {
            dgvTramDung.AutoGenerateColumns = false;
            dgvTramDung.DataSource = tdBUS.TramDung();
        }

        private void Load_Tram_Dung(string ma_con_duong)
        {
            dgvTramDung.AutoGenerateColumns = false;
            dgvTramDung.DataSource = tdBUS.TramDung(ma_con_duong);
        }

        private void btThemMoi_Click(object sender, EventArgs e)
        {
            frmThemSuaTramDung ttd = new frmThemSuaTramDung();
            if (ma_con_duong != null)
                ttd.cbbMaConDuong.Text = ma_con_duong;
            ttd.ShowDialog();

            if (ma_con_duong != null)
                Load_Tram_Dung(ma_con_duong);
            else
                Load_Tram_Dung();
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            try
            {
                frmThemSuaTramDung ttd = new frmThemSuaTramDung();
                ttd.label6.Text = "Sửa trạm dừng: mã số " + dt.Rows[0]["Ma_Tram_Dung"].ToString();
                ttd.ma_tram_dung = dt.Rows[0]["Ma_Tram_Dung"].ToString();
                ttd.txtSoHieu.Text = dt.Rows[0]["So_Hieu_Tram"].ToString();
                ttd.txtTenTram.Text = dt.Rows[0]["Ten_Tram"].ToString();
                ttd.txtDiaChiTram.Text = dt.Rows[0]["Dia_Chi_Tram"].ToString();
                ttd.txtViTriDL.Text = dt.Rows[0]["Vi_Tri_Dia_Ly"].ToString();
                ttd.cbbMaConDuong.Text = dt.Rows[0]["Ma_Con_Duong"].ToString();

                ttd.ShowDialog();
                if (ma_con_duong != null)
                    Load_Tram_Dung(ma_con_duong);
                else
                    Load_Tram_Dung();
            }
            catch (Exception) { }
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có chắc chắn muốn xóa không ? ", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                try
                {
                    y = dgvTramDung.CurrentCell.RowIndex;
                    string ma_tram_dung = dgvTramDung.Rows[y].Cells[0].Value.ToString();
                    tdBUS.XOA(ma_tram_dung);
                    MessageBox.Show("Xóa dữ liệu thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception)
                {
                    MessageBox.Show("Xóa dữ liệu thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                if (ma_con_duong != null)
                    Load_Tram_Dung(ma_con_duong);
                else
                    Load_Tram_Dung();
            }
        }

        private void btDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Hien_Thi(int y)
        {
            string id = dgvTramDung.Rows[y].Cells[0].Value.ToString();
            dt = tdBUS.TramDungTheoID(id);

            lblMaTramDung.Text = dt.Rows[0]["Ma_Tram_Dung"].ToString();
            lblSoHieuTram.Text = dt.Rows[0]["So_Hieu_Tram"].ToString();
            lblTenTram.Text = dt.Rows[0]["Ten_Tram"].ToString();
            lblDiaChiTram.Text = dt.Rows[0]["Dia_Chi_Tram"].ToString();
            rtbViTriDL.Text = dt.Rows[0]["Vi_Tri_Dia_Ly"].ToString();
            lblMaConDuong.Text = dt.Rows[0]["Ma_Con_Duong"].ToString();
        }

        private void dgvTramDung_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            y = e.RowIndex;
            Hien_Thi(y);
        }
    }
}
